package com.lovo.project2.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.project2.bean.PermissionBean;
import com.lovo.project2.bean.RoleBean;
import com.lovo.project2.mapper.IRoleBeanMapper;
import com.lovo.project2.service.IRoleBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleBeanServiceImpl extends ServiceImpl<IRoleBeanMapper, RoleBean> implements IRoleBeanService {
    @Autowired
   IRoleBeanMapper iRoleBeanMapper;


    @Override
    public IPage queryAllRolePage(IPage iPage) {

        return baseMapper.selectPage(iPage,null);
    }

    @Transactional
    @Override
    public void removeRole(int roleId) {
        iRoleBeanMapper.deleteByRoleIdPermissions(roleId);
baseMapper.deleteById(roleId);
    }

    @Override
    public RoleBean selectRoleBean(int roleId) {

        return iRoleBeanMapper.selectByRoleIdRoleBean(roleId);
    }

    @Transactional
    @Override
    public void updateRoleBean(int roleId, List<Integer> list) {
iRoleBeanMapper.updateRoleBean(roleId);
iRoleBeanMapper.insert(roleId,list);

    }
@Transactional
    @Override
    public void addRoleBean(RoleBean roleBean) {
    iRoleBeanMapper.addRole(roleBean);
        List<Integer> list=new ArrayList<>();
   for (PermissionBean permissionBean:roleBean.getPerLists()) {
       list.add(permissionBean.getPerId());
   }
        iRoleBeanMapper.insert(roleBean.getRoleId(),list);
      ;

    }
}
